package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.aid.AidInventory;
import com.ruoyi.system.service.aid.IAidInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存管理Controller
 */
@RestController
@RequestMapping("/aid/inventory")
public class AidInventoryController extends BaseController
{
    @Autowired
    private IAidInventoryService inventoryService;

    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(AidInventory inventory)
    {
        startPage();
        List<AidInventory> list = inventoryService.selectInventoryList(inventory);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:export')")
    @Log(title = "库存管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AidInventory inventory)
    {
        List<AidInventory> list = inventoryService.selectInventoryList(inventory);
        ExcelUtil<AidInventory> util = new ExcelUtil<AidInventory>(AidInventory.class);
        util.exportExcel(response, list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:query')")
    @GetMapping(value = "/{inventoryId}")
    public AjaxResult getInfo(@PathVariable("inventoryId") Long inventoryId)
    {
        return success(inventoryService.selectInventoryById(inventoryId));
    }

    /**
     * 新增库存
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:add')")
    @Log(title = "库存管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AidInventory inventory)
    {
        return toAjax(inventoryService.insertInventory(inventory));
    }

    /**
     * 修改库存
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:edit')")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AidInventory inventory)
    {
        return toAjax(inventoryService.updateInventory(inventory));
    }

    /**
     * 删除库存
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:remove')")
    @Log(title = "库存管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inventoryIds}")
    public AjaxResult remove(@PathVariable Long[] inventoryIds)
    {
        return toAjax(inventoryService.deleteInventoryByIds(inventoryIds));
    }

    /**
     * 根据仓库ID查询库存
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:query')")
    @GetMapping("/warehouse/{warehouseId}")
    public AjaxResult getByWarehouse(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(inventoryService.selectInventoryByWarehouseId(warehouseId));
    }

    /**
     * 根据物资名称查询库存
     */
    @PreAuthorize("@ss.hasPermi('aid:inventory:query')")
    @GetMapping("/material")
    public AjaxResult getByMaterial(String materialName)
    {
        return success(inventoryService.selectInventoryByMaterialName(materialName));
    }
} 